# -*- coding: utf-8 -*-
# @Time    : 2019/3/29 11:02
# @Author  : zjj
# @Email   : 1933860854@qq.com
# @File    : OppoBrower.py
# @Software: PyCharm
import scrapy
import json
import time
class IQiYiVideo(scrapy.Spider):
    name = 'iqiyi_video_ads_spider'
    allowed_domains = ['cards.iqiyi.com']
    start_urls = ['https://cards.iqiyi.com/views_home/3.0/qy_home?prev_card_index=27&ad_skip=6&filter_id_4_recommend_4_you=202807401,202807401,226174401,226174401,233071001,2313388800,236732201,2302319300,553769102,554306002,216889001,215316401,205486401,553839402,227765601,405604002,105085601,220366001,228029301,206293401,222870801,225915601,361553000,226174401,235674901,2200683100,140753900,226174401,216889001,202807401,551972702,220366001,216427801,239578601,219548601,238418701,223460001,2311164500,553315102,2319702100,2317321800,554305402,551559102,2317234600,2308665700,2220060900,2306718700,214955601,222904001,238834701,228415301,229622801,90927301,123796101,202246601,102071200,103873101,2290548800,2168900200,988985900,1228241300,405604002,2169903300,704194800,1482038400,2157743600,228696600,231586501,235322401,230805501,203280301,218084801,218014601,205776601,202943801,226806901,230419701&page_st=&pg_num=3&sei=Sdcd363af0a05b7ec355575866ad8e1d2&bi_params=%7B%22yysuggest%22%3A%22nosuggest%22%2C%22rank_rise%22%3A%22riseA%22%2C%22firstcard%22%3A%22rec%22%2C%22top_type%22%3A%22new_hot_alltags%22%2C%22focus%22%3A%22BI%22%2C%22vip_private%22%3A%22BI%22%2C%22is_poster%22%3A%22operation%22%2C%22bi_recommend_reason%22%3A%22norecreason%22%2C%22channel_type%22%3A%22nomal1%22%2C%22movie_private%22%3A%22BI%22%7D&from_block=E:020000&from_rpage=home_top_menu&card_list=R:204835212-wx,R:204836612,R:204836512,R:204835512,R:204835612,R:664139312,R:205174712,R:204837212,R:204835812,R:204837312,R:204836412,R:204837112,R:204836712,R:204835912,R:9190350412,R:207952912,R:206074412,R:204837012,R:12569584112,R:204836112,0:0200000175,R:204836912,R:204836212,R:204836312,R:204835712,R:204836012,R:16206077012,W:02819510001&ad_start_pos=4&from_rseat=0&card_v=3.0&lang=zh_CN&app_lm=cn&province_id=3001&qyidv2=7A79516CA4D78CFBAD5E025FA1321DEE&last_message_id=1904182280010734074&show_hotspot=1&houyi_ab=7_B,8_B,12_B,13_B,20_A,25_B,26_A,30_C,31_A,33_C&app_k=456ddd1ff52a6089c7e3f806d609520d&app_v=10.3.5&platform_id=10&dev_os=5.1.1&dev_ua=MI+6+&net_sts=1&qyid=863254010762044&cupid_v=3.35.002&psp_uid=&psp_cki=&imei=b6b93baa5ba4c67eb1b75a7b5000a3a4&aid=4ccc6a9ef4863990&mac=4C:CC:6A:9E:F4:86&scrn_scale=2&secure_p=GPhone&secure_v=1&core=1&api_v=8.8&profile=%7B%22group%22%3A%221%2C2%22%2C%22counter%22%3A1%7D&service_filter=&service_sort=&layout_v=44.115&device_type=0&timestamp=1552612594344&app_v_pre=&js_enable=1&cupid_uid=863254010762044&psp_status=1&app_gv=&gps=,&bdgps=&req_times=0&req_sn=1556071173213']
    headers = {
        'qyid': '863254010762044_4ccc6a9ef4863990_4CZCCZ6AZ9EZF4Z86',
        'Connection': 'Keep-Alive',
        't': '512097056',
        'sign': '7d899e0c7d7cd8515d830a3ce03fb822',
        'Host': 'cards.iqiyi.com',
        'Accept-Encoding': 'gzip'
    }

    def start_requests(self):
        while True:
            for url in self.start_urls:
                yield scrapy.Request(url, method='GET', headers=self.headers, callback=self.parse, dont_filter=True)

    def parse(self, response):
        html = response.text
        if html:
            result_json = json.loads(html)
            next_url = result_json['base']['next_url']
            result_list = result_json['cards']
            for result in result_list:
                if '信息流广告大图-动态位置-10.1' == result['name']:
                    print(result)
            yield scrapy.Request(url=self.start_urls[0], headers=self.headers, dont_filter=True)



if __name__ == '__main__':
    from scrapy import cmdline
    cmdline.execute("scrapy crawl iqiyi_video_ads_spider".split())